package CanvaWeb.CanvaWeb.Controller;

import CanvaWeb.CanvaWeb.Service.DrawingService;
import CanvaWeb.CanvaWeb.Service.GameService;
import CanvaWeb.CanvaWeb.pojo.Drawing;
import CanvaWeb.CanvaWeb.pojo.DrawingRequest;
import CanvaWeb.CanvaWeb.pojo.DrawingResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/room/{roomId}/drawings")
public class DrawingController {

    @Autowired
    private DrawingService drawingService;

    @Autowired
    private GameService gameService; // 引入游戏服务来处理游戏逻辑

    // 上传绘画数据，包含提示信息
    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadDrawing(
            @PathVariable("roomId") Long roomId,
            @RequestBody DrawingRequest drawingRequest) {

        Map<String, Object> response = new HashMap<>();
        try {
            // 保存绘画数据和提示
            drawingService.saveDrawing(drawingRequest.getPlayerName(), drawingRequest.getDrawingData(), roomId, drawingRequest.getHint(),drawingRequest.getWord());

            response.put("success", true);
            response.put("message", "绘画上传成功");

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "绘画上传失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    // 获取最新的绘画数据，包含提示信息
    @GetMapping("/latest")
    public ResponseEntity<Map<String, Object>> getLatestDrawings(@PathVariable("roomId") Long roomId) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 获取指定房间内的绘画列表
            List<Drawing> drawings = drawingService.getDrawingsByRoom(roomId);

            // 如果有绘画数据，取最新的一幅
            if (!drawings.isEmpty()) {
                Drawing latestDrawing = drawings.get(drawings.size() - 1); // 获取最新的绘画

                // 构建返回的响应
                DrawingResponse drawingResponse = new DrawingResponse(
                        latestDrawing.getPlayerName(),
                        latestDrawing.getDrawingData(),
                        latestDrawing.getHint(),
                        latestDrawing.getWord()
                );

                // 添加 success 字段和消息
                response.put("success", true);
                response.put("message", "获取绘画成功");
                response.put("drawingData", drawingResponse);
            } else {
                response.put("success", false);
                response.put("message", "没有绘画数据");
            }

            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取绘画数据失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
}





